﻿// ***********************************************************************
// Assembly         : OSS.Model
// Author           : tianteng
// Created          : 04-10-2018
//
// Last Modified By : tianteng
// Last Modified On : 04-10-2018
// ***********************************************************************
// <copyright file="ModelFileDbUtil.cs" company="北京智帆高科科技有限公司">
//     Copyright ©  2017
// </copyright>
// <summary></summary>
// ***********************************************************************

using System.Collections.Generic;
using System.Linq;
using OSS.BIM.Model;
using ZFGK.Utility;

namespace OSS.BIM.Business.Utility
{
    /// <summary>
    /// Class ModelFileDbUtil.
    /// </summary>
    public static class ModelFileDbUtil
    {
        /// <summary>
        /// Gets at.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns>ModelFile.</returns>
        public static ModelFile GetAt(int id)
        {
            return DB.Context.From<ModelFile>().Where(d => d.ModelFileId == id).First();
        }
        /// <summary>
        /// Gets all.
        /// </summary>
        /// <returns>ModelFile[].</returns>
        public static ModelFile[] GetAll()
        {
            return DB.Context.From<ModelFile>().ToList().ToArray();
        }
        /// <summary>
        /// Gets the last.
        /// </summary>
        /// <param name="count">The count.</param>
        /// <returns>ModelFile[].</returns>
        public static ModelFile[] GetLast(int count)
        {
            return DB.Context.From<ModelFile>().OrderByDescending(ModelFile._.ModelFileId).Top(count).ToList().ToArray();
        }

        /// <summary>
        /// Gets at by fingerprint.
        /// </summary>
        /// <param name="Fingerprint">The fingerprint.</param>
        /// <returns>ModelFile.</returns>
        public static ModelFile GetAtByFingerprint(string Fingerprint)
        {
            return DB.Context.From<ModelFile>().Where(d => d.Fingerprint == Fingerprint).First();
        }

        /// <summary>
        /// News the identifier if not set.
        /// </summary>
        /// <param name="modelFile">The model file binary.</param>
        public static void NewIdIfNotSet(this ModelFile modelFile)
        {
            if (modelFile.ModelFileId == 0)
                modelFile.ModelFileId = SnowflakeUtil.GetId();
        }

        /// <summary>
        /// Inserts the specified model file.
        /// </summary>
        /// <param name="modelFile">The model file.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public static bool Insert(ModelFile modelFile)
        {
            modelFile.NewIdIfNotSet();
            return DB.Context.Insert(modelFile) > 0;
        }

        /// <summary>
        /// Inserts the specified model files.
        /// </summary>
        /// <param name="modelFiles">The model files.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public static bool Insert(IEnumerable<ModelFile> modelFiles)
        {
            modelFiles.ForEach(d => d.NewIdIfNotSet());
            return DB.Context.Insert(modelFiles) > 0;
        }

        /// <summary>
        /// Gets the last identifier.
        /// </summary>
        /// <returns>System.Int32.</returns>
        public static long GetLastId()
        {
            return DB.Context.From<ModelFile>().Select(d => d.ModelFileId)
                .OrderByDescending(ModelFile._.ModelFileId).Top(1).First().ModelFileId;
        }

        /// <summary>
        /// Gets the model.
        /// </summary>
        /// <param name="modelElement">The model information.</param>
        /// <returns>System.Byte[].</returns>
        public static ModelFile[] GetModelFile(this ModelElement modelElement)
        {
            return DB.Context.From<ModelFile>().Where(d => d.ModelElementId == modelElement.ModelElementId).ToEnumerable().ToArray();
        }

        /// <summary>
        /// Gets the file binary.
        /// </summary>
        /// <param name="modelFile">The model file.</param>
        /// <returns>ModelFileBinary.</returns>
        public static ModelFileBinary GetFileBinary(this ModelFile modelFile)
        {
            return DB.Context.From<ModelFileBinary>().Where(d => d.ModelFileId == modelFile.ModelFileId).First();
        }
    }
}
